package com.guaoran.micro.services.spring.cloud.eureka.client.user.service.provider.web.controller;

import com.guaoran.micro.services.spring.cloud.eureka.client.user.service.provider.service.PersonService;
import com.guaoran.micro.services.spring.cloud.feign.person.api.domain.Person;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;

/**
 * {@link com.guaoran.micro.services.spring.cloud.feign.person.api.service.PersonService} 提供者控制器（可选实现{@link com.guaoran.micro.services.spring.cloud.feign.person.api.service.PersonService}）
 *
 */
@RestController
public class PersonServiceProviderController {

    private final Map<Long, Person> persons = new ConcurrentHashMap<>();
    private final static Random random = new Random();


    @Autowired
    private PersonService personService;
    /**
     * 保存
     *
     * @param person {@link Person}
     * @return 如果成功，<code>true</code>
     */
    @PostMapping(value = "/person/save")
    public boolean savePerson(@RequestBody Person person) {
        com.guaoran.micro.services.spring.cloud.eureka.
                client.user.service.provider.entity.Person personSave =
                new com.guaoran.micro.services.spring.cloud.eureka.
                        client.user.service.provider.entity.Person();
        BeanUtils.copyProperties(person,personSave);
        personService.save(personSave);
        return true;
    }

    /**
     * 分页查询
     * @param pageable
     * @return
     */
    @GetMapping("/person/list")
    public Page<com.guaoran.micro.services.spring.cloud.eureka.client.user.service.provider.entity.Person> list(Pageable pageable){
        return personService.findListByPage(pageable);

    }

    /**
     * 查找所有的服务
     *
     * @return
     */
    @GetMapping(value = "/person/find/all")
    @HystrixCommand(fallbackMethod = "fallbackForFindAllPersons",
            commandProperties = {
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",
                            value = "100")
            }
    )
    public Collection<Person> findAllPersons() throws InterruptedException {

        // 如果随机时间 大于 100 ，那么触发容错
        int value = random.nextInt(200);
        Thread.sleep(value);
        System.out.println("findAllPersons() costs " + value + " ms.");
        return persons.values();
    }

    /**
     * {@link #findAllPersons()} Fallback 方法
     *
     * @return 返回空集合
     */
    public Collection<Person> fallbackForFindAllPersons() {
        System.err.println("fallbackForFindAllPersons() is invoked!");
        return Collections.emptyList();
    }
}
